home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python-1.4 / Lib / dis.py < prev    next >
Text File  |  1998-06-24  |  5KB  |  202 lines

  1. # Disassembler
  2.  
  3. import sys
  4. import string
  5.  
  6. def dis():
  7.     tb = sys.last_traceback
  8.     while tb.tb_next: tb = tb.tb_next
  9.     distb(tb)
  10.  
  11. def distb(tb):
  12.     disassemble(tb.tb_frame.f_code, tb.tb_lasti)
  13.  
  14. def disco(co):
  15.     disassemble(co, -1)
  16.  
  17. def disassemble(co, lasti):
  18.     code = co.co_code
  19.     labels = findlabels(code)
  20.     n = len(code)
  21.     i = 0
  22.     while i < n:
  23.         c = code[i]
  24.         op = ord(c)
  25.         if op == SET_LINENO and i > 0: print # Extra blank line
  26.         if i == lasti: print '-->',
  27.         else: print '   ',
  28.         if i in labels: print '>>',
  29.         else: print '  ',
  30.         print string.rjust(`i`, 4),
  31.         print string.ljust(opname[op], 15),
  32.         i = i+1
  33.         if op >= HAVE_ARGUMENT:
  34.             oparg = ord(code[i]) + ord(code[i+1])*256
  35.             i = i+2
  36.             print string.rjust(`oparg`, 5),
  37.             if op in hasconst:
  38.                 print '(' + `co.co_consts[oparg]` + ')',
  39.             elif op in hasname:
  40.                 print '(' + co.co_names[oparg] + ')',
  41.             elif op in hasjrel:
  42.                 print '(to ' + `i + oparg` + ')',
  43.             elif op in haslocal:
  44.                 print '(' + co.co_varnames[oparg] + ')',
  45.         print
  46.  
  47. def findlabels(code):
  48.     labels = []
  49.     n = len(code)
  50.     i = 0
  51.     while i < n:
  52.         c = code[i]
  53.         op = ord(c)
  54.         i = i+1
  55.         if op >= HAVE_ARGUMENT:
  56.             oparg = ord(code[i]) + ord(code[i+1])*256
  57.             i = i+2
  58.             label = -1
  59.             if op in hasjrel:
  60.                 label = i+oparg
  61.             elif op in hasjabs:
  62.                 label = oparg
  63.             if label >= 0:
  64.                 if label not in labels:
  65.                     labels.append(label)
  66.     return labels
  67.  
  68. hasconst = []
  69. hasname = []
  70. hasjrel = []
  71. hasjabs = []
  72. haslocal = []
  73.  
  74. opname = [''] * 256
  75. for op in range(256): opname[op] = '<' + `op` + '>'
  76.  
  77. def def_op(name, op):
  78.     opname[op] = name
  79.  
  80. def name_op(name, op):
  81.     opname[op] = name
  82.     hasname.append(op)
  83.  
  84. def jrel_op(name, op):
  85.     opname[op] = name
  86.     hasjrel.append(op)
  87.  
  88. def jabs_op(name, op):
  89.     opname[op] = name
  90.     hasjabs.append(op)
  91.  
  92. # Instruction opcodes for compiled code
  93.  
  94. def_op('STOP_CODE', 0)
  95. def_op('POP_TOP', 1)
  96. def_op('ROT_TWO', 2)
  97. def_op('ROT_THREE', 3)
  98. def_op('DUP_TOP', 4)
  99.  
  100. def_op('UNARY_POSITIVE', 10)
  101. def_op('UNARY_NEGATIVE', 11)
  102. def_op('UNARY_NOT', 12)
  103. def_op('UNARY_CONVERT', 13)
  104. def_op('UNARY_CALL', 14)
  105. def_op('UNARY_INVERT', 15)
  106.  
  107. def_op('BINARY_POWER', 19)
  108. def_op('BINARY_MULTIPLY', 20)
  109. def_op('BINARY_DIVIDE', 21)
  110. def_op('BINARY_MODULO', 22)
  111. def_op('BINARY_ADD', 23)
  112. def_op('BINARY_SUBTRACT', 24)
  113. def_op('BINARY_SUBSCR', 25)
  114. def_op('BINARY_CALL', 26)
  115.  
  116. def_op('SLICE+0', 30)
  117. def_op('SLICE+1', 31)
  118. def_op('SLICE+2', 32)
  119. def_op('SLICE+3', 33)
  120.  
  121. def_op('STORE_SLICE+0', 40)
  122. def_op('STORE_SLICE+1', 41)
  123. def_op('STORE_SLICE+2', 42)
  124. def_op('STORE_SLICE+3', 43)
  125.  
  126. def_op('DELETE_SLICE+0', 50)
  127. def_op('DELETE_SLICE+1', 51)
  128. def_op('DELETE_SLICE+2', 52)
  129. def_op('DELETE_SLICE+3', 53)
  130.  
  131. def_op('STORE_SUBSCR', 60)
  132. def_op('DELETE_SUBSCR', 61)
  133.  
  134. def_op('PRINT_EXPR', 70)
  135. def_op('PRINT_ITEM', 71)
  136. def_op('PRINT_NEWLINE', 72)
  137.  
  138. def_op('BREAK_LOOP', 80)
  139. def_op('RAISE_EXCEPTION', 81)
  140. def_op('LOAD_LOCALS', 82)
  141. def_op('RETURN_VALUE', 83)
  142. def_op('EXEC_STMT', 85)
  143. def_op('BUILD_FUNCTION', 86)
  144. def_op('POP_BLOCK', 87)
  145. def_op('END_FINALLY', 88)
  146. def_op('BUILD_CLASS', 89)
  147.  
  148. HAVE_ARGUMENT = 90        # Opcodes from here have an argument: 
  149.  
  150. name_op('STORE_NAME', 90)    # Index in name list 
  151. name_op('DELETE_NAME', 91)    # "" 
  152. def_op('UNPACK_TUPLE', 92)    # Number of tuple items 
  153. def_op('UNPACK_LIST', 93)    # Number of list items 
  154. def_op('UNPACK_ARG', 94)    # Number of arguments expected
  155. name_op('STORE_ATTR', 95)    # Index in name list 
  156. name_op('DELETE_ATTR', 96)    # ""
  157. name_op('STORE_GLOBAL', 97)    # ""
  158. name_op('DELETE_GLOBAL', 98)    # ""
  159. name_op('UNPACK_VARARG', 99)    # Minimal number of arguments
  160.  
  161. def_op('LOAD_CONST', 100)    # Index in const list 
  162. hasconst.append(100)
  163. name_op('LOAD_NAME', 101)    # Index in name list 
  164. def_op('BUILD_TUPLE', 102)    # Number of tuple items 
  165. def_op('BUILD_LIST', 103)    # Number of list items 
  166. def_op('BUILD_MAP', 104)    # Always zero for now 
  167. name_op('LOAD_ATTR', 105)    # Index in name list 
  168. def_op('COMPARE_OP', 106)    # Comparison operator 
  169. name_op('IMPORT_NAME', 107)    # Index in name list 
  170. name_op('IMPORT_FROM', 108)    # Index in name list 
  171.  
  172. jrel_op('JUMP_FORWARD', 110)    # Number of bytes to skip 
  173. jrel_op('JUMP_IF_FALSE', 111)    # "" 
  174. jrel_op('JUMP_IF_TRUE', 112)    # "" 
  175. jabs_op('JUMP_ABSOLUTE', 113)    # Target byte offset from beginning of code 
  176. jrel_op('FOR_LOOP', 114)    # Number of bytes to skip 
  177.  
  178. name_op('LOAD_LOCAL', 115)    # Index in name list
  179. name_op('LOAD_GLOBAL', 116)    # Index in name list
  180. def_op('SET_FUNC_ARGS', 117)    # Argcount
  181.  
  182. jrel_op('SETUP_LOOP', 120)    # Distance to target address
  183. jrel_op('SETUP_EXCEPT', 121)    # ""
  184. jrel_op('SETUP_FINALLY', 122)    # ""
  185.  
  186. def_op('RESERVE_FAST', 123)    # Number of local variables
  187. hasconst.append(123)
  188. def_op('LOAD_FAST', 124)    # Local variable number
  189. haslocal.append(124)
  190. def_op('STORE_FAST', 125)    # Local variable number
  191. haslocal.append(125)
  192. def_op('DELETE_FAST', 126)    # Local variable number
  193. haslocal.append(126)
  194.  
  195. def_op('SET_LINENO', 127)    # Current line number
  196. SET_LINENO = 127
  197.  
  198. def_op('RAISE_VARARGS', 130)
  199. def_op('CALL_FUNCTION', 131)
  200. def_op('MAKE_FUNCTION', 132)
  201. def_op('BUILD_SLICE', 133)
  202.